/**
 * Created by dukes on 2015/10/21.
 */
class SceneMgr extends egret.HashObject{
    public static SCENE_MAIN:number = 0;
    public static SCENE_TRAVEL:number = 1;
    public static SCENE_SHOP:number = 2;
    public static SCENE_GETOUT:number = 3;
    public static SCENE_DATA:number = 4;
    public static SCENE_OTHERS:number = 5;
    public static SCENE_ACTIVITY:number = 6;
    public static SCENE_SOCIATY:number = 7;
    public static SCENE_DECORATE:number = 8;
    public static SCENE_BATTLE:number = 9;
    public static SCENE_CHAT:number = 10;
    public static SCENE_CHAT_P:number = 11;
    public static SCENE_MAIL:number = 12;
    public currentScene:number=-1;
    public lastScene:number = -1;
    private tempData:any;
    constructor(){
        super();
    }
    public switchScene(scene:number,data:any=null){
        if( Model.inDating){
            return;
        }
        this.tempData = data;
        if(this.currentScene != scene){
            this.cleanCurrentScene();
            this.lastScene = this.currentScene;
            this.currentScene = scene;
            switch (scene){
                case SceneMgr.SCENE_MAIN:
                    this.showMainScene();
                    break;
                case SceneMgr.SCENE_TRAVEL:
                    this.showTravelScene();
                    break;
                case SceneMgr.SCENE_SHOP:
                    this.showShopScene();
                    break;
                case SceneMgr.SCENE_GETOUT:
                    this.showDateScene();
                    break;
                case SceneMgr.SCENE_DATA:
                    this.showDataScene();
                    break;
                case SceneMgr.SCENE_OTHERS:
                    this.showMainScene(1);
                    break;
                case SceneMgr.SCENE_ACTIVITY:
                    this.showActivityScene();
                    break;
                case SceneMgr.SCENE_DECORATE:
                    this.showDecorateScene();
                    break;
                case SceneMgr.SCENE_BATTLE:
                    this.showBattleScene();
                    break;
                case SceneMgr.SCENE_CHAT:
                    this.showChatScene();
                    break;
                case SceneMgr.SCENE_CHAT_P:
                    this.showChatPersonalScene();
                    break;
                case SceneMgr.SCENE_MAIL:
                    this.showMailScene();
                    break;
                case SceneMgr.SCENE_SOCIATY:
                    this.showSociatyScene();
                    break;
            }
        }
    }

    public cleanCurrentScene(){
        CleanHint();
        if( this.currentScene != -1){//todo ��Щ������Ҫ�Ż�
            switch (this.currentScene){
                case SceneMgr.SCENE_MAIN:
                    this.cleanMainScene();
                    break;
                case SceneMgr.SCENE_TRAVEL:
                case SceneMgr.SCENE_SHOP:
                case SceneMgr.SCENE_DATA:
                case SceneMgr.SCENE_ACTIVITY:
                    this.cleanTravelScene();
                    break;
         
                case SceneMgr.SCENE_DECORATE:
                    this.cleanDecorateScene();
                    break;
                case SceneMgr.SCENE_GETOUT:
                    this.cleanGetOutScene();
                    break;
                case SceneMgr.SCENE_BATTLE:
                    this.cleanBattleScene();
                    break;
                case SceneMgr.SCENE_CHAT:
                    this.cleanChatScene();
                    break;
                case SceneMgr.SCENE_CHAT_P:
                    this.cleanChatPersonalScene();
                    break;
                case SceneMgr.SCENE_MAIL:
                    this.cleanMailScene();
                    break;
                case SceneMgr.SCENE_SOCIATY:
                    this.cleanSociatyScene();
                    break;
            }
        }
    }
    public returnToLastScene(){
        if( this.lastScene != -1){
            this.switchScene(this.lastScene);
        }
    }

    public cleanBattleScene(){
        DisplayController.instance().removeUiElement(BattleScene.getInstance());
        DisplayController.instance().addUiElement(MainTab.getInstance());
        SoundMgr.getInstance().playBg();
    }

    public cleanChatScene(){
        DisplayController.instance().removeUiElement(ChatScene.getInstance());
        DisplayController.instance().addUiElement(MainTab.getInstance());
    }
    public cleanChatPersonalScene(){
        DisplayController.instance().removeUiElement(PersonalChatScene.getInstance());
    }

    public showBattleScene(){
        if( RES.isGroupLoaded("battle")){
            if(this.currentScene != SceneMgr.SCENE_BATTLE ) return;
            DisplayController.instance().addUiElement(BattleScene.getInstance());
            DisplayController.instance().removeUiElement(MainTab.getInstance());
            DisplayController.instance().removeUiElement(PlayBar.getInstance());
            if( this.tempData ){
                BattleScene.getInstance().setBattleData(this.tempData[0],this.tempData[1],this.tempData[2],this.tempData[3],this.tempData[4],this.tempData[5]);
            }else if( Model.REVENGE_DATA){
                BattleScene.getInstance().setBattleData(Model.REVENGE_DATA.petWar,Model.REVENGE_DATA.bePillagePet, Model.REVENGE_DATA.hair,1,Model.REVENGE_DATA.bePillageHarm,Model.REVENGE_DATA.pillageHarm)
            }
            if (Model.getStateFromMask(GameConst.MARK_AUTO_BATTLE)){
                    BattleScene.getInstance().start();
            }else{
                BattleInstruction.getInstance().show();
            }
        }else{
            RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
            window["__showConnection"](true);
            if(  Model.getStateFromMask(GameConst.MARK_AUTO_BATTLE) ){
                var group:Array<RES.ResourceItem> = RES.getGroupByName("battle");
                var len = group.length;
                var resItem:RES.ResourceItem;
                for(var i = 0 ; i < len; i++){
                    resItem = group[i];
                    if( resItem.name=="battleinstructions_json"){
                        group.splice(i,1);
                        break;
                    }
                }
            }
            RES.loadGroup("battle");
        }
    }

    public showChatScene(){
        if( RES.isGroupLoaded("chat")){
            if( this.currentScene != SceneMgr.SCENE_CHAT ) {
                return;
            }
            DisplayController.instance().addUiElement(ChatScene.getInstance());
            if( this.tempData){
                setTimeout(function(){
                    ChatScene.getInstance().forceDisplay(this.tempData);
                },800);
            }
            DisplayController.instance().removeUiElement(MainTab.getInstance());
            DisplayController.instance().removeUiElement(PlayBar.getInstance());
            this.tempData = null;
        }else{
            RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
            window["__showConnection"](true);
            RES.loadGroup("chat");
        }
    }
    public showSociatyScene(){
    }
    public cleanSociatyScene(){
    }
    public showChatPersonalScene(){
        DisplayController.instance().addUiElement(PersonalChatScene.getInstance());
        PersonalChatScene.getInstance().updateChater(this.tempData[0]);
    }
    public showMailScene(){
        DisplayController.instance().removeUiElement(MainTab.getInstance());
        DisplayController.instance().removeUiElement(PlayBar.getInstance());
        DisplayController.instance().addUiElement(MailScene.getInstance());
    }
    public cleanMailScene(){
        DisplayController.instance().removeUiElement(MailScene.getInstance());
        DisplayController.instance().addUiElement(MainTab.getInstance());
    }

    public showMainScene(type:number=0){
        if( type == 0 ) {//my home
            DisplayController.instance().addUiElement(MainScene.getInstance());
            DisplayController.instance().addUiElement(PlayBar.getInstance());
            //PlayBar.getInstance().visible = true;
            PlayBar.getInstance().showPage();
            PlayBar.getInstance().switchModel(0);
            if( PetMgr.getInstance().bagPostion == null && DiamondElement.getInstance().parent){//todo
                PetMgr.getInstance().initBagPosition(DiamondElement.getInstance().img_money_0.localToGlobal(),DiamondElement.getInstance().img_money_1.localToGlobal());
            }
            PetMgr.getInstance().showMain(0);
            if( DiamondElement.getInstance().parent)
                DiamondElement.getInstance().updateDisplay();
            DisplayController.instance().bringElementToTop(MainTab.getInstance());
        }else{//others home
            DisplayController.instance().removeUiElement(MainScene.getInstance());
            PetMgr.getInstance().showMain(1);
            PetMgr.getInstance().showOthersPet(this.tempData);
        }
    }

    public cleanTravelScene(){
        DisplayController.instance().removeUiElement(PanelContainer.getInstance());
    }

    public showTravelScene(evt:RES.ResourceEvent=null){
        if( RES.isGroupLoaded("travelpanel")){
            if( this.currentScene != SceneMgr.SCENE_TRAVEL) return;
            DisplayController.instance().addUiElement(PanelContainer.getInstance());
            PanelContainer.getInstance().showPanel(SceneMgr.SCENE_TRAVEL );
            PetMgr.getInstance().cleanMain();
            DisplayController.instance().bringElementToTop(MainTab.getInstance());
            //RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
        }else{
            if(  !Model.UnlockConfigs)
                window["__unlocks"]();
            RES.loadGroup("travelpanel");
            RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
        }
    }

    public showShopScene(){
        DisplayController.instance().addUiElement(PanelContainer.getInstance());
        PanelContainer.getInstance().showPanel( SceneMgr.SCENE_SHOP );
    }

    
    public showDateScene(){
        if( RES.isGroupLoaded("getout")){
            if( this.currentScene != SceneMgr.SCENE_GETOUT) return;
            PetMgr.getInstance().cleanMain();
            DisplayController.instance().addUiElement(GetoutScene.getInstance());
            if( this.tempData ){
                setTimeout(function(){
                    DateScene.getInstance().forceDisplay(arguments[0]);
                },500,this.tempData)
            }

            DisplayController.instance().bringElementToTop(MainTab.getInstance());
            RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
        }else{
            window["__showConnection"](true);
            window["__dateInfo"]();
            RES.loadGroup("getout");
            RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
        }
    }

    public showDataScene(){
        if( RES.isGroupLoaded("bigdata")){
            if( this.currentScene != SceneMgr.SCENE_DATA) return;
            DisplayController.instance().addUiElement(PanelContainer.getInstance());
            PanelContainer.getInstance().showPanel( SceneMgr.SCENE_DATA );
            PetMgr.getInstance().cleanMain();
        }else{
            RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
            window["__showConnection"](true);
            RES.loadGroup("bigdata");
        }
    }

    public showActivityScene(){
        if( RES.isGroupLoaded("activity")){
            if( this.currentScene != SceneMgr.SCENE_ACTIVITY) return;
            DisplayController.instance().addUiElement(PanelContainer.getInstance());
            PanelContainer.getInstance().showPanel( SceneMgr.SCENE_ACTIVITY );
            PetMgr.getInstance().cleanMain();
        }else{
            RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
            window["__showConnection"](true);
            RES.loadGroup("activity");
        }
    }

    public showDecorateScene(){
        if( RES.isGroupLoaded("decorate")){
            DisplayController.instance().addUiElement(PlayBar.getInstance());
            DisplayController.instance().addUiElement(DecorateScene.getInstance());
            PlayBar.getInstance().switchModel(1);
            PetMgr.getInstance().showMain(2);
        }else{
            RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
            window["__showConnection"](true);
            RES.loadGroup("decorate");
        }

    }

    public cleanActivityScene(){
        DisplayController.instance().removeUiElement(ActivityPanel.getInstance());
    }

    public cleanRechargeScene(){
        DisplayController.instance().removeUiElement(BuyDiamondPanel.getInstance());
    }

    public cleanMainScene(){
        PetMgr.getInstance().cleanMain();
        DisplayController.instance().removeUiElement(PlayBar.getInstance());
        DisplayController.instance().removeUiElement(MainScene.getInstance());
    }

    public cleanDecorateScene(){
        PetMgr.getInstance().cleanMain();
        DisplayController.instance().removeUiElement(PlayBar.getInstance());
        DisplayController.instance().removeUiElement(DecorateScene.getInstance());
        DecorateScene.getInstance().checkSavable();
    }

    public cleanGetOutScene(){
        PetMgr.getInstance().cleanMain();
        DisplayController.instance().removeUiElement(GetoutScene.getInstance());
    }

    private onResourceLoadComplete(event:RES.ResourceEvent):void {
        if( event.groupName == "activity"){
            window["__showConnection"](false);
            this.showActivityScene();
            //RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
        }else if( event.groupName == "battle"){
            window["__showConnection"](false);
            this.showBattleScene();
            //RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
        }else if( event.groupName == "chat"){
            window["__showConnection"](false);
            this.showChatScene();
            //RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
        }else if( event.groupName == "travelpanel"){
            window["__showConnection"](false);
            this.showTravelScene(event);
        }else if( event.groupName == "getout"){
            window["__showConnection"](false);
            this.showDateScene();
        }else if(event.groupName == "decorate"){
            window["__showConnection"](false);
            this.showDecorateScene();
        }else if( event.groupName == "bigdata"){
            window["__showConnection"](false);
            this.showDataScene();
        }
    }

    private static _instance:SceneMgr;
    public static getInstance():SceneMgr{
        if( SceneMgr._instance == null)
            SceneMgr._instance = new SceneMgr();
        return SceneMgr._instance;
    }
}